package com.situ.stl.dao;

import com.situ.stl.entity.Admin;
import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/** 
 * Dao层 - 管理员
 */
@Mapper 
public interface AdminMapper{

    /** 
     * Insert
     */
    @Insert( value = "insert into `admin` ( `phone`, `password`, `name`) value ( #{phone}, #{password}, #{name})")
    @Options( useGeneratedKeys = true, keyColumn = "id", keyProperty = "id") 
    int insert( Admin admin) ;

    /** 
     * Delete
     */
    @Delete( value = "delete from `admin` where `id` = #{id}") 
    int delete( Integer id) ;

    /** 
     * Update
     */
    @Update( value = {
        "<script>",
        "update `admin`",
        "<set>",
        "<if test='phone!=null and phone.length>0'> `phone` = #{phone},</if>",
        "<if test='password!=null and password.length>0'> `password` = #{password},</if>",
        "<if test='name!=null'> `name` = #{name},</if>",
        "</set>",
        " where `id` = #{id}",
        "</script>"}) 
    int update( Admin admin) ;

    /** 
     * Select All
     */
    @Select( value = "select * from `admin`") 
    @Results( value = {
        @Result( column = "id", property = "id", id = true),
        @Result( column = "phone", property = "phone"),
        @Result( column = "password", property = "password"),
        @Result( column = "name", property = "name"),
        })
    List<Admin> selectAll() ;

    /** 
     * Select by primary key
     */
    @Select( value = "select * from `admin` where `id` = #{id}") 
    @Results( value = {
        @Result( column = "id", property = "id", id = true),
        @Result( column = "phone", property = "phone"),
        @Result( column = "password", property = "password"),
        @Result( column = "name", property = "name"),})
    Admin selectByPrimaryKey( int id) ;

    /** 
     * Select
     */
    @Select( value = {
        "<script>",
        "select * from `admin`",
        "<where>",
        "<if test='phone!=null and phone.length>0'> and `phone` like '%${phone}%'</if>",
        "<if test='password!=null and password.length>0'> and `password` like '%${password}%'</if>",
        "<if test='name!=null and name.length>0'> and `name` like '%${name}%'</if>",
        "</where>",
        "</script>"}) 
    @Results( value = {
        @Result( column = "id", property = "id", id = true),
        @Result( column = "phone", property = "phone"),
        @Result( column = "password", property = "password"),
        @Result( column = "name", property = "name"),})
    List<Admin> select( Admin admin) ;

    /** 
     * 为登录而生
     */
    @Select( value = {
        "select * from `admin`",
        "where `phone` = #{phone}"}) 
    @Results( value = {
        @Result( column = "id", property = "id", id = true),
        @Result( column = "phone", property = "phone"),
        @Result( column = "password", property = "password"),
        @Result( column = "name", property = "name"),})
    Admin selectByPhone( String phone) ;

}